/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.digwener.ui;

import com.digwener.entities.connectors.*;
import com.digwener.db.*;
import javax.swing.JComboBox;

/**
 *
 * @author Anton
 */
public class InstanceList {
    
    private DBConnection dbConnection = DBConnection.getInstance();
    
    private JComboBox combo = new JComboBox();
    
    private ConnectorHolder connectorHolder;
    
    private String defaultInstance = "";
    
    public JComboBox getCombo(){
        return this.combo;
    }

    public ConnectorHolder getConnectorHolder() {
        return connectorHolder;
    }

    public void setConnectorHolder(ConnectorHolder connectorHolder) {
        this.connectorHolder = connectorHolder;
    }
    
    public void fillList(){
        combo.removeAllItems();
        connectorHolder.loadFromXML();
        for(ConnectorType connType:connectorHolder.getConnectorsList()){
            combo.addItem(connType.getInstanceName());
        }
        if(!defaultInstance.isEmpty()){
            combo.setSelectedItem(defaultInstance);
        }
    }
    
    public ConnectorType getSelected(){
        int ind = combo.getSelectedIndex();
        System.out.println("ind:"+ind);
        if(ind == -1)
            return null;
        int n = connectorHolder.getConnectorsList().size();
        if(ind>=n){
            return null;
        }
        
        return connectorHolder.getConnectorsList().get(ind);
        
    }
    
    public void initConnector(){
        ConnectorType connType = getSelected();
        if(connType != null){
            System.out.println(connType.getInstanceName());
            dbConnection.setConnector(connType);
            dbConnection.connect();
        }
    }
      
}
